package org.eclipseuseradmin.ui.wizards;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipseuseradmin.internal.ui.UserAdminUiPlugin;
import org.eclipseuseradmin.internal.ui.wizards.WizardsMessages;
import org.eclipseuseradmin.ui.model.UserModel;

public class NewUserWizardPage extends NewRoleElementWizardPage {

	protected Label passwordLabel;

	protected Text passwordText;

	protected Label confirmPasswordLabel;

	protected Text confirmPasswordText;

	public NewUserWizardPage() {

		this("main", WizardsMessages.get().NewUserWizardPage_title,
				WizardsMessages.get().NewUserWizardPage_description,
				UserAdminUiPlugin.getImageDescriptor("/icons/user_wiz.gif"));
	}

	public NewUserWizardPage(String pageName, String title, String description,
			ImageDescriptor titleImage) {

		super(pageName, title, description, titleImage);
	}

	@Override
	public void createControl(Composite parent) {
		super.createControl(parent);

		passwordLabel = new Label(mainComposite, SWT.NONE);
		passwordLabel.setText(WizardsMessages.get().NewUserWizardPage_password);

		passwordText = new Text(mainComposite, SWT.BORDER | SWT.PASSWORD);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		passwordText.setLayoutData(layoutData);
		bindPasswordValue();

		confirmPasswordLabel = new Label(mainComposite, SWT.NONE);
		confirmPasswordLabel
				.setText(WizardsMessages.get().NewUserWizardPage_confirmPassword);

		confirmPasswordText = new Text(mainComposite, SWT.BORDER | SWT.PASSWORD);
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		confirmPasswordText.setLayoutData(layoutData);
		bindConfirmPasswordValue();

	}

	protected void bindPasswordValue() {

		UserModel model = (UserModel) getNewRoleElementWizard().getModel();

		UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
		updateValueStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {

				if (((String) value).length() == 0) {
					return ValidationStatus
							.error(WizardsMessages.get().NewUserWizardPage_error_password_empty);
				}
				if (((String) value).length() < 5) {
					return ValidationStatus
							.error(WizardsMessages.get().NewUserWizardPage_error_password_length);
				}
				return ValidationStatus.OK_STATUS;
			}
		});
		Binding binding = getBindingContext().bindValue(
				SWTObservables.observeText(passwordText, SWT.FocusOut),
				PojoObservables.observeValue(model, "password"),
				updateValueStrategy, null);
	}

	protected void bindConfirmPasswordValue() {

		final UserModel model = (UserModel) getNewRoleElementWizard()
				.getModel();

		UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
		updateValueStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {

				if (((String) value).length() == 0) {
					return ValidationStatus
							.error(WizardsMessages.get().NewUserWizardPage_error_password_empty);
				}
				if (!((String) value).equals(model.getPassword())) {
					return ValidationStatus.error(WizardsMessages.get().NewUserWizardPage_error_confirmPassword_match);
				}
				return ValidationStatus.OK_STATUS;
			}
		});
		Binding binding = getBindingContext().bindValue(
				SWTObservables.observeText(confirmPasswordText, SWT.FocusOut),
				PojoObservables.observeValue(model, "confirmPassword"),
				updateValueStrategy, null);
	}

}
